<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('User_model');
        $this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->library('session');

        function alpha_dash_space($str) {
            return (!preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
        }

    }

    protected $validation_rules = array('registration' => array(array('field' => 'name', 'label' => 'Name', 'rules' => 'trim|xss_clean|callback__alpha_dash_space'), array('field' => 'username', 'label' => 'Username', 'rules' => 'trim|required'), array('field' => 'email', 'label' => 'Email', 'rules' => 'required|valid_email|callback_check_email'), array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required')));
    protected $editvalidation_rules = array('editregistration' => array(array('field' => 'name', 'label' => 'Name', 'rules' => 'trim|required'), array('field' => 'email', 'label' => 'Email', 'rules' => 'required|valid_email'), array('field' => 'password', 'label' => 'Password', 'rules' => 'trim|required')));
    protected $editownvalidation_rules = array('editregistration' => array(array('field' => 'name', 'label' => 'Name', 'rules' => 'trim|required'), array('field' => 'phone', 'label' => 'phone', 'rules' => 'required|numeric|exact_length[10]')));

    function alpha_dash_space($str) {
        return (!preg_match("/^([-a-z_ ])+$/i", $str)) ? FALSE : TRUE;
    }

    public function index() {
        $type = $this->session->all_userdata();
        $type = $type['0'];
        $type = $type->user_type;
        if ($type != 'admin') {
            $this->show_view('permission');
        } else {
            $this->data['user'] = $this->User_model->getUser();
            $this->show_view('user', $this->data);
        }
    }

    function image() {
        $this->data['user'] = $this->User_model->getImage();
        $this->show_view('header', $this->data);
    }

    function saveUser() {
        if (isset($_POST['submit']) && $_POST['submit'] = "Add") {
            $data = array();
            $data['name'] = $this->input->post('name');
            $data['username'] = $this->input->post('username');
            $data['email'] = $this->input->post('email');
            $data['password'] = $this->input->post('password');
            $data['user_type'] = 'admin';
            $data['create_time'] = date('Y-m-d');

            $this->form_validation->set_rules($this->validation_rules['registration']);
            if ($this->form_validation->run()) {

                $result = count($this->User_model->check_user($data['username']));


                if ($result == 0) {
                    $this->User_model->saveUser($data);
                    redirect('user');
                } else {

                    $this->show_view('adduser');
                }
            } else {
                $this->show_view('adduser');
            }
        } else {
            $this->show_view('adduser');
        }
    }

    function DeleteUser() {
        $this->User_model->DeleteUser('$id');
    }

    function editUser() {
        if (isset($_POST['submit'])) {
            $data['name'] = $_POST['name'];
            $data['email'] = $_POST['email'];
            $data['password'] = $_POST['password'];

            $this->form_validation->set_rules($this->editvalidation_rules['editregistration']);
            if ($this->form_validation->run()) {
                $s = $this->User_model->updateUser($data);
                if ($s == 1) {
                    $this->session->set_flashdata('msg', 'user successfully updated');
                    redirect(base_url() . 'user');
                } else {
                    $this->session->set_flashdata('msg', 'user Not updated');
                    redirect(base_url() . 'user/edituser?id=' . $_POST['id']);
                }
            } else {
                $data['user'] = $this->User_model->editUser();
                $this->show_view('edituser', $data);
            }
        } else {
            $data['user'] = $this->User_model->editUser();
            $this->show_view('edituser', $data);
        }
    }

    function checkUser($name) {
        $result = $this->User_model->check_user($name);
        echo count($result);
    }

    function check_User() {
        $username = $this->input->post('user');
        $result = $this->User_model->check_user($username);
        echo count($result);
    }

    public function setStatus() {
        $post_data = array();
        $post_data['id'] = $this->input->post('id');
        $post_data['status'] = $this->input->post('status');
        //$post_data['modify_date'] = date('Y-m-d');
        $this->User_model->setStatus($post_data);
        echo 1;
        exit();
    }

    function changepassword() {
        $type = $this->session->all_userdata();
        $pass = $type['0'];
        $pass = $pass->password;
        if (isset($_POST['submit'])) {
            if ($_POST['oldpassword'] != $pass) {
                $this->session->set_flashdata('msg', 'Enter Your Old Password Correct');
                redirect(base_url() . 'user/changepassword');
            }
            if ($_POST['newpassword'] != $_POST['confirmpassword']) {
                $this->session->set_flashdata('msg', 'Confirm Password is not Match');
                redirect(base_url() . 'user/changepassword');
            }
            $data['password'] = $_POST['newpassword'];
            $this->User_model->changePassword($data);
        }
        $this->show_view('changepassword');
    }

    function check_email($str) {
        $email = $this->User_model->check_email($str);
        if ($email) {
            $this->form_validation->set_message('check_email', 'Email Already Exist');
            return FALSE;
        } else {
            return TRUE;
        }
    }

    function profile() {
        $this->data['user'] = $this->User_model->profile();
        $this->show_view('profile', $this->data);
    }

    function do_upload() {
        $type = $this->session->all_userdata();
        $type = $type['0'];

        //print_r($_FILES); DIE;
        $fileExt = end(explode('.',$_FILES['image']['name']));
        $allowedSize = 2; //MB
        $allowedFileSize = $allowedSize * 1024 * 1024; // Bytes
        $allowedExt = array('jpg', 'jpeg', 'png', 'bmp');
        $extns = implode(',', $allowedExt);
        $p_image = $type->username.".jpg";
        //echo $ext; die;
        if($_FILES['image']['size'] <= $allowedFileSize)
      {
        // print_r( $fileInfoArray = pathinfo($p_image));
        $src = $_FILES['image']['tmp_name'];
        $dest = "images/$p_image";
       
         if(in_array($fileExt,$allowedExt)) {
        $isMoved = move_uploaded_file($src, $dest);
        if ($isMoved == true) {
            $this->User_model->do_upload();
            $this->session->set_flashdata('msg', 'Image Changed Successfully');
            redirect(base_url() . 'user/profile');
        } else {
            $this->session->set_flashdata('error', 'Image not uploaded');
            redirect(base_url() . 'user/profile');
        }
        
        } else {
             $this->session->set_flashdata('error', 'Please upload file with valid extn '.($extns));
             redirect(base_url() . 'user/profile');
          echo "";
        }
      }  else {
         $this->session->set_flashdata('error', 'Please upload file with MAX '.$allowedSize.'MB');
            redirect(base_url() . 'user/profile'); 
      }
    }

    function editProfile() {
        if (isset($_POST['submit'])) {
            $data['name'] = $_POST['name'];
            $data['email'] = $_POST['email'];
            $data['password'] = $_POST['password'];

            $this->form_validation->set_rules($this->editvalidation_rules['editregistration']);
            if ($this->form_validation->run()) {
                $s = $this->User_model->updateUser($data);
                if ($s == 1) {
                    $this->session->set_flashdata('msg', 'user successfully updated');
                    redirect(base_url() . 'user');
                } else {
                    $this->session->set_flashdata('msg', 'user Not updated');
                    redirect(base_url() . 'user/edituser?id=' . $_POST['id']);
                }
            } else {
                $data['user'] = $this->User_model->editUser();
                $this->show_view('edituser', $data);
            }
        } else {
            $data['user'] = $this->User_model->editUser();
            $this->show_view('edituser', $data);
        }
    }

    function editown() {
        if (isset($_POST['submit'])) {
            unset($_POST['submit']);
            $data = $_POST;
            // print_r($data);
            $this->form_validation->set_rules($this->editownvalidation_rules['editregistration']);
            if ($this->form_validation->run()) {
                $this->User_model->Editpro($data);
                $this->session->set_flashdata('msg', 'Profile Successfully Updated');
                redirect(base_url() . 'user/profile');
            } else {
                $data['user'] = $this->User_model->editown();
                $this->show_view('editown', $data);
            }
        } else {
            $data['user'] = $this->User_model->editown();
            //print_r($data);
            $this->show_view('editown', $data);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */